package com.fastloan.jn.mgt.process.service.impl;

import com.fastloan.jn.common.BasicServiceImpl;
import com.fastloan.jn.mgt.process.dao.JnAppApproveFileMapper;
import com.fastloan.jn.mgt.process.model.JnAppApproveFile;
import com.fastloan.jn.mgt.process.model.vo.AppApproveFileVO;
import com.fastloan.jn.mgt.process.service.AppApproveFileService;
import com.kawins.supers.file.IFileManager;
import com.kawins.supers.table.manager.DataTable;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.Map;

import static java.util.stream.Collectors.toList;

/**
 * 审核文件相关Service
 * Created by chenkai on 2017/4/17.
 */
@Service
public class AppApproveFileServiceImpl extends BasicServiceImpl<JnAppApproveFile> implements AppApproveFileService {

    @Autowired
    private JnAppApproveFileMapper approveFileDao;

    @Autowired
    private IFileManager fileManager;

    @Override
    @DataTable(name = "queryFile")
    public List<AppApproveFileVO> queryFile(Map<String, Object> map) {
        String applicationId = (String) map.get("applicationId");
        String appApproveUserId = (String) map.get("appApproveUserId");
        JnAppApproveFile appApproveFile = new JnAppApproveFile();
        appApproveFile.setApplicationId(applicationId);
        if (!StringUtils.isBlank(appApproveUserId)) {
            appApproveFile.settAppApproveId(appApproveUserId);
            map.put("tAppApproveId", appApproveUserId);
        }
        List<String> fileIds = approveFileDao.select(appApproveFile)
                .stream()
                .map(JnAppApproveFile::getFileId)
                .collect(toList());
        if (!fileIds.isEmpty()) {
            map.put("fileIds", fileIds);
        }
        map.put("applicationId", applicationId);
        List<AppApproveFileVO> approveFileVOList = approveFileDao.queryFile(map);
        approveFileVOList.forEach(appApprove -> appApprove.setFileUrl(File.separator + appApprove.gettFile().getGroupName()
                + File.separator + appApprove.gettFile().getRemoteFileName()));
        return approveFileVOList;
    }

    @Override
    @Transactional
    public int upload(MultipartFile[] files, String appApproveUserId, String applicationId) {
        return fileManager.upload(files)
                .stream()
                .mapToInt(fileLoadBean -> {
                    JnAppApproveFile appApproveFile = new JnAppApproveFile();
                    appApproveFile.settAppApproveId(appApproveUserId);
                    appApproveFile.setFileId(fileLoadBean.getFileId().toString());
                    appApproveFile.setApplicationId(applicationId);
                    return approveFileDao.insert(appApproveFile);
                }).sum();
    }

    @Override
    @Transactional
    public int deleteFile(String fileId) {
        JnAppApproveFile appApproveFile = new JnAppApproveFile();
        appApproveFile.setFileId(fileId);
        fileManager.delete(fileId);
        return approveFileDao.delete(appApproveFile);
    }

}
